import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)
// 保证在页面刷新后从localStorage取值然后存入vuex
let userInfo = JSON.parse(localStorage.getItem('userInfo')) || {}
export default new Vuex.Store({
  state: {
    // 用户信息
    userInfo: userInfo
  },
  getters: {
    menuList(state) {
      return state.userInfo.menus
    }
  },
  mutations: {
    // 保存用户信息
    setUserInfo(state, payload) {
      state.userInfo = payload;
      // 同步地在localStorage也存一份
      localStorage.setItem('userInfo', JSON.stringify(payload));
    },
    // 退出登录，清除信息
    clearUserInfo(state) {
      state.userInfo = {};
      // 同步地在localStorage也存一份
      localStorage.setItem('userInfo', '');
    },
  },
  actions: {
  },
  modules: {
  }
})
